package com.xydtech.ecard.busi.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiReprotInfo;
import com.xydtech.ecard.busi.domain.EcardApiBusiReprotRecord;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportConfigService;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子名片业务填报类型配置Controller
 *
 * @author yuzhiguo
 * @date 2024-09-25 下午1:39
 */
@Controller
@Slf4j
@Api(tags = "电子名片-业务填报类型配置接口")
@RequestMapping("/ecardSaasApi/busi/reportConfig")
public class EcardApiBusiReportConfigController extends EcardApiBaseController {

    @Autowired
    private IEcardApiBusiReportConfigService ecardBusiReportConfigService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 电子名片-查询法人机构下业务填报类型配置列表接口
     *
     * @author yuzhiguo
     * @date 2024/9/25 下午1:44
     */
    @PostMapping(value = "/getReportConfigList")
    @ApiOperation(value = "电子名片-查询法人机构下业务填报类型配置列表接口", notes = "电子名片-查询法人机构下业务填报类型配置列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getReportConfigList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            Integer busiType = jsonObject.getInteger("busiType");//业务填报分类：1-个人类，2-绑卡类，3-对公类，4-其他业务
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //获取登录人县级机构
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());

            EcardApiBusiReportConfig ecardBusiReportConfig = new EcardApiBusiReportConfig();
            ecardBusiReportConfig.setDeptId(sysDept.getDeptId());
            ecardBusiReportConfig.setBusiType(busiType);

            List<EcardApiBusiReportConfig> busiReportConfigList = ecardBusiReportConfigService.getReportConfigList(ecardBusiReportConfig);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(busiReportConfigList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询查询登录人名下的业务填报记录接口
     *
     * @author yuzhiguo
     * @date 2024/9/29 上午11:03
     */
    @PostMapping(value = "/getMyBusiReportList")
    @ApiOperation(value = "电子名片-条件查询查询登录人名下的业务填报记录接口", notes = "电子名片-条件查询查询登录人名下的业务填报记录接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMyBusiReportList(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            Integer busiType = jsonObject.getInteger("busiType");//业务填报分类：1-个人类，2-绑卡类，3-对公类，4-其他业务
            String busiName = jsonObject.getString("busiName");//业务名称
            String startDate = jsonObject.getString("startDate");//查询起日
            String endDate = jsonObject.getString("endDate");//查询止日
            Map<String, Object> map = new HashMap<>();
            map.put("startDate", startDate);
            map.put("endDate", endDate);
            EcardApiBusiReprotRecord busiReprotRecord = new EcardApiBusiReprotRecord();
            busiReprotRecord.setManagementPersonnelId(Long.valueOf(currentUserId));
            busiReprotRecord.setBusiType(busiType);
            busiReprotRecord.setBusiName(busiName);
            busiReprotRecord.setParams(map);

            List<EcardApiBusiReprotRecord> busiReprotRecordList = ecardBusiReportConfigService.getMyBusiReportList(busiReprotRecord);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(busiReprotRecordList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询查询登录人名下的业务列表接口
     *
     * @author yuzhiguo
     * @date 2024/9/29 下午1:04
     */
    @PostMapping(value = "/getMyBusiReportInfo")
    @ApiOperation(value = "电子名片-条件查询查询登录人名下的业务列表接口", notes = "电子名片-条件查询查询登录人名下的业务列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMyBusiReportInfo(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            Integer busiType = jsonObject.getInteger("busiType");//业务填报分类：1-个人类，2-绑卡类，3-对公类，4-其他业务
            String busiCode = jsonObject.getString("busiCode");//业务唯一标识
            String custName = jsonObject.getString("custName");//联系人
            //查询条件姓名和手机号拆开
            String custPhone = jsonObject.getString("custPhone");//联系电话
            String startDate = jsonObject.getString("startDate");//查询起日
            String endDate = jsonObject.getString("endDate");//查询止日
            Map<String, Object> map = new HashMap<>();
            map.put("startDate", startDate);
            map.put("endDate", endDate);
            EcardApiBusiReprotRecord busiReprotRecord = new EcardApiBusiReprotRecord();
            busiReprotRecord.setManagementPersonnelId(Long.valueOf(currentUserId));
            busiReprotRecord.setBusiType(busiType);
            busiReprotRecord.setBusiCode(busiCode);
            busiReprotRecord.setCustName(custName);
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(custPhone)) {
                busiReprotRecord.setCustPhone(AESEncryptionUtils.encrypt(custPhone, salt));
            }
            busiReprotRecord.setParams(map);
            List<EcardApiBusiReprotRecord> busiReprotRecordList = ecardBusiReportConfigService.getMyBusiReportInfo(busiReprotRecord, salt);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSONArray(busiReprotRecordList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 电子名片-条件查询查询登录人名下的业务详情接口
     *
     * @author yuzhiguo
     * @date 2024/10/10 上午10:09
     */
    @PostMapping(value = "/getMyBusiReportDetail")
    @ApiOperation(value = "电子名片-条件查询查询登录人名下的业务详情接口", notes = "电子名片-条件查询查询登录人名下的业务详情接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMyBusiReportDetail(@RequestBody String json) {
        log.info("接口请求时间-------" + DateUtils.getTime());
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            JSONObject jsonObject = JSONObject.parseObject(json);
            Integer busiType = jsonObject.getInteger("busiType");//业务填报分类：1-个人类，2-绑卡类，3-对公类，4-其他业务
            Integer id = jsonObject.getInteger("id");//业务id
            String configId = jsonObject.getString("configId");//业务配置主键id

            EcardApiBusiReprotInfo busiReprotInfo = new EcardApiBusiReprotInfo();
            busiReprotInfo.setId(Long.valueOf(id));
            busiReprotInfo.setBusiType(busiType);
            busiReprotInfo.setConfigId(configId);
            EcardApiBusiReprotInfo ecardBusiReprotInfo = ecardBusiReportConfigService.getMyBusiReportDetail(busiReprotInfo, salt);
            log.info("接口返回时间-------" + DateUtils.getTime());
            return ResponseModel.okToJSON(ecardBusiReprotInfo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
}
